/*
 * Ingenic libsysutils LED implement.
 *
 * Copyright (C) 2014 Ingenic Semiconductor Co.,Ltd
 * Author: Zoro <ykli@ingenic.cn>
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#include <sysutils/su_misc.h>
#include <imp/imp_log.h>

#define TAG		"Test_Led"

#if 1
#define PROC_LED_PATH "/proc/board/power/led"

static int sys_write(const char *path, const char *cmd)
{
	int fd = open(path, O_WRONLY);
	if (fd < 0) {
		printf("open %s error: %s", path, strerror(errno));
		return -1;
	}

	int ret = write(fd, cmd, strlen(cmd));
	if (!(ret == strlen(cmd))) {
		printf("write %s error: %s", path, strerror(errno));
		return -1;
	}

	close(fd);
	return 0;
}

int SU_LED_Command(int ledNum, SULedCmd cmd)
{
	char path[64];
	sprintf(path, "%s%d", PROC_LED_PATH, ledNum);

	return sys_write(path, cmd ? "1" : "0");
}

#else
int SU_LED_Command(SULedInfo *ledInfo, SULedCmd cmd)
{
	int value;
	int fd_export, fd_active_low, fd_direction, fd_value;
#define TMP_BUF_SIZE 64
	char buf[TMP_BUF_SIZE];

	/* open export */
	fd_export = open("/sys/class/gpio/export", O_WRONLY);
	if(fd_export < 0) {
		IMP_LOG_ERR(TAG, "open() export error !");
		return -1;
	}

	memset(buf, 0, TMP_BUF_SIZE);
	snprintf(buf, TMP_BUF_SIZE, "%d", ledInfo->gpioNum);

	write(fd_export, buf, TMP_BUF_SIZE);

	close(fd_export);

	/* open active_low */
	memset(buf, 0, TMP_BUF_SIZE);
	snprintf(buf, TMP_BUF_SIZE, "%s%d/%s", "/sys/class/gpio/gpio",
			 ledInfo->gpioNum, "active_low");

	fd_active_low = open(buf, O_WRONLY);
	if(fd_active_low < 0) {
		IMP_LOG_ERR(TAG, "open() active_low error !");
		return -1;
	}

	memset(buf, 0, TMP_BUF_SIZE);
	snprintf(buf, TMP_BUF_SIZE, "%d", !ledInfo->enableLevel);

	write(fd_active_low, buf, TMP_BUF_SIZE);

	close(fd_active_low);

	/* open direction */
	memset(buf, 0, TMP_BUF_SIZE);
	snprintf(buf, TMP_BUF_SIZE, "%s%d/%s", "/sys/class/gpio/gpio",
			 ledInfo->gpioNum, "direction");

	fd_direction = open(buf, O_WRONLY);
	if(fd_direction < 0) {
		IMP_LOG_ERR(TAG, "open() direction error !");
		return -1;
	}

	memset(buf, 0, TMP_BUF_SIZE);
	snprintf(buf, TMP_BUF_SIZE, "%s", "out");

	write(fd_direction, buf, TMP_BUF_SIZE);

	close(fd_direction);

	/* open value */
	memset(buf, 0, TMP_BUF_SIZE);
	snprintf(buf, TMP_BUF_SIZE, "%s%d/%s", "/sys/class/gpio/gpio",
			 ledInfo->gpioNum, "value");

	fd_value = open(buf, O_WRONLY);
	if(fd_value < 0) {
		IMP_LOG_ERR(TAG, "open() value error !");
		return -1;
	}

	value = !!cmd;

	memset(buf, 0, TMP_BUF_SIZE);
	snprintf(buf, TMP_BUF_SIZE, "%d", value);

	write(fd_value, buf, TMP_BUF_SIZE);

	close(fd_value);
#if 0
	/* open unexport */
	int fd_unexport;
	fd_unexport = open("/sys/class/gpio/unexport", O_WRONLY);
	if(fd_unexport < 0) {
		IMP_LOG_ERR(TAG, "open() unexport error !");
		return -1;
	}

	memset(buf, 0, TMP_BUF_SIZE);
	snprintf(buf, TMP_BUF_SIZE, "%d", gpio_num);

	write(fd_unexport, buf, TMP_BUF_SIZE);

	close(fd_unexport);
#endif
	return 0;
}
#endif
